<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.9.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->
    
        <meta name="description" content="链接：🔗
https://www.runoob.com/django/django-nginx-uwsgi.html

yum update
yum install gcc
pip install uwsgi

123456789101112131415161718192021查看uwsgi动态链">
    

    <!--Author-->
    
        <meta name="author" content="ck">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="Django+Nginx+uWSGI安装配置">
    

    <!--Open Graph Description-->
    

    <!--Open Graph Site Name-->
    <meta property="og:site_name" content="CK">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    

    <meta name="twitter:card" content="summary">
    

    <!-- Title -->
    
    <title>Django+Nginx+uWSGI安装配置 - CK</title>

    <!-- Bootstrap Core CSS -->
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" integrity="sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd" crossorigin="anonymous">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Google Analytics -->
    


</head>


<body>

<div class="bg-gradient"></div>
<div class="bg-pattern"></div>

<!-- Menu -->
<!--Menu Links and Overlay-->
<div class="menu-bg">
    <div class="menu-container">
        <ul>
            
            <li class="menu-item">
                <a href="/blog/">
                    Home
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/archives">
                    Archives
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/about.html">
                    About
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/tags">
                    Tags
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/categories">
                    Categories
                </a>
            </li>
            
            <li class="menu-item">
                <a href="/blog/contact.html">
                    Contact
                </a>
            </li>
            
        </ul>
    </div>
</div>

<!--Hamburger Icon-->
<nav>
    <a href="#menu"></a>
</nav>

<div class="container">

    <!-- Main Content -->
    <div class="row">
    <div class="col-sm-12">

        <!--Title and Logo-->
        <header>
    <div class="logo">
        <a href="/blog/"><i class="logo-icon fa fa-cube" aria-hidden="true"></i></a>
        
    </div>
</header>

        <section class="main">
            
<div class="post">

    <div class="post-header">
        <h1 class="title">
            <a href="/blog/2019/08/12/Django-Nginx-uWSGI安装配置/">
                Django+Nginx+uWSGI安装配置
            </a>
        </h1>
        <div class="post-info">
            
                <span class="date">2019-08-12</span>
            
            
            
        </div>
    </div>

    <div class="content">

        <!-- Gallery -->
        

        <!-- Post Content -->
        <p>链接：🔗</p>
<p><a href="https://www.runoob.com/django/django-nginx-uwsgi.html" target="_blank" rel="noopener">https://www.runoob.com/django/django-nginx-uwsgi.html</a></p>
<blockquote>
<p>yum update</p>
<p>yum install gcc</p>
<p>pip install uwsgi</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">查看uwsgi动态链接库是否有问题：</span><br><span class="line">which  uwsgi</span><br><span class="line">ldd /root/anaconda3/bin/uwsgi</span><br><span class="line">发现:libicui18n.so.58 =&gt; not found</span><br><span class="line">     libicuuc.so.58 =&gt; not found</span><br><span class="line">     libicudata.so.58 =&gt; not found</span><br><span class="line">通过 LDD 发现 uwsgi 找不到三个包（具体就是 not found 的三个）</span><br><span class="line">于是就把anaconda3/lib 下相应的包软链到/lib64下去（64bit 机器，如果时32bit 机器则软链到/lib下）</span><br><span class="line"></span><br><span class="line">[root@localhost ~]# ln -s /root/anaconda3/lib/libicui18n.so.58 /lib64/libicui18n.so.58</span><br><span class="line">[root@localhost ~]# ln -s /root/anaconda3/lib/libicuuc.so.58 /lib64/libicuuc.so.58</span><br><span class="line">[root@localhost ~]# ln -s /root/anaconda3/lib/libicudata.so.58 /lib64/libicudata.so.58</span><br><span class="line">运行uwsgi:发现还有问题</span><br><span class="line">于是：</span><br><span class="line">    把libstdc++.so.6.0.24拷贝到/lib64目录下。</span><br><span class="line">      cp /root/anaconda3/lib/libstdc++.so.6.0.25 /lib64/</span><br><span class="line">    删除原来的libstdc++.so.6符号连接。</span><br><span class="line">      rm -fr /lib64/libstdc++.so.6</span><br><span class="line">    新建新符号连接。</span><br><span class="line">      ln -s /lib64/libstdc++.so.6.0.25 /lib64/libstdc++.so.6</span><br><span class="line">    再次执行uwsgi查看结果符合就哦了。</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">测试：创建index.py</span><br><span class="line">def application(env, start_response):</span><br><span class="line">    start_response(&apos;200 OK&apos;, [(&apos;Content-Type&apos;,&apos;text/html&apos;)])</span><br><span class="line">    return [b&quot;Hello World&quot;]</span><br><span class="line"></span><br><span class="line">uwsgi --http :9090 --wsgi-file index.py</span><br><span class="line">浏览器访问9090端口</span><br></pre></td></tr></table></figure>

<h6 id="添加并发"><a href="#添加并发" class="headerlink" title="添加并发"></a>添加并发</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">uwsgi --http :9090 --wsgi-file index.py --processes 4 --threads 2</span><br></pre></td></tr></table></figure>

<h6 id="部署-Django"><a href="#部署-Django" class="headerlink" title="部署 Django"></a>部署 Django</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">#在项目中创建djangospider.ini</span><br><span class="line">[uwsgi]</span><br><span class="line"># 使用Nginx做反向代理时使用socket</span><br><span class="line">#socket=0.0.0.0:8000   </span><br><span class="line"># 不通过Nginx反向代理，直接做web服务器时使用http</span><br><span class="line">http = 0.0.0.0:8000</span><br><span class="line">#项目目录</span><br><span class="line">chdir = /usr/www/DjangoSpider/</span><br><span class="line">wsgi-file = DjangoSpider/wsgi.py</span><br><span class="line">processes = 4</span><br><span class="line">threads = 2</span><br><span class="line"></span><br><span class="line">#django项目setting中</span><br><span class="line">ALLOWED_HOSTS = [] 改为</span><br><span class="line">ALLOWED_HOSTS = [&apos;*&apos;]</span><br><span class="line"></span><br><span class="line">启动：uwsgi --ini  djangospider.ini</span><br></pre></td></tr></table></figure>



<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">在www中创建uwsgi文件夹，用来存放uwsgi相关文件</span><br><span class="line">#### 启动： </span><br><span class="line">uwsgi --ini xxx.ini</span><br><span class="line">#### 重启：</span><br><span class="line">uwsgi --reload xxx.pid</span><br><span class="line">#### 停止：</span><br><span class="line">uwsgi --stop xxx.pid</span><br></pre></td></tr></table></figure>

<h1 id="配置Nginx"><a href="#配置Nginx" class="headerlink" title="配置Nginx"></a>配置Nginx</h1><p><strong>nginx服务器安装：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">下载对应当前系统版本的nginx包(package)</span><br><span class="line"># wget  http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm</span><br><span class="line"></span><br><span class="line">建立nginx的yum仓库</span><br><span class="line"># rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm</span><br><span class="line"></span><br><span class="line">下载并安装nginx</span><br><span class="line"># yum install -y nginx</span><br><span class="line"></span><br><span class="line">启动nginx服务</span><br><span class="line">systemctl start/restart nginx</span><br><span class="line">验证：直接请求IP，可见nginx</span><br><span class="line"></span><br><span class="line">配置</span><br><span class="line">默认的配置文件在 /etc/nginx 路径下，使用该配置已经可以正确地运行nginx；如需要自定义，修改其下的 nginx.conf 等文件即可。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">查看是否启动：（如果有master和worker两个进程证明启动成功）</span><br><span class="line">ps -ef | grep nginx</span><br><span class="line"></span><br><span class="line">停止(建议用quit，不建议用stop)</span><br><span class="line">nginx -s quit</span><br><span class="line"></span><br><span class="line">重新加载配置文件</span><br><span class="line">nginx -s reload</span><br><span class="line"></span><br><span class="line">默认配置文件位置</span><br><span class="line">/etc/nginx/conf.d/default.conf</span><br><span class="line">备份默认配置文件</span><br><span class="line">cp  /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak</span><br><span class="line"></span><br><span class="line">welcome所在位置（默认值：/usr/share/nginx/html/）</span><br></pre></td></tr></table></figure>



<p><strong>修改nginx配置文件：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"># 负载均衡</span><br><span class="line">upstream upstream1 &#123;</span><br><span class="line">    server linux——ip:8000;  # 处理相同业务的多个服务器 (负载均衡)</span><br><span class="line">&#125;</span><br><span class="line">server &#123;</span><br><span class="line">    listen       80;</span><br><span class="line">    server_name  localhost;   # 对外部用户提供的统一域名</span><br><span class="line"></span><br><span class="line">    # 将动态请求转发给uwsgi服务器</span><br><span class="line">    location / &#123;</span><br><span class="line">        # 包含uwsgi请求的参数</span><br><span class="line">        include uwsgi_params;</span><br><span class="line">        # 转交请求给uwsgi服务器 (uwsgi服务器的ip和端口号)</span><br><span class="line">        #uwsgi_pass 127.0.0.1:8000;  # 单个uwsgi服务器(不需要负载均衡)</span><br><span class="line">        uwsgi_pass upstream1;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    # 如果是静态资源请求就直接响应资源。</span><br><span class="line">    location /static &#123;</span><br><span class="line">        # 指定静态文件存放的目录 (需要修改/usr/...目录的操作权限。 $ chmod 777 /usr)</span><br><span class="line">        alias /usr/www/项目名/static/;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>验证：</strong></p>
<p>nginx -s reload</p>
<p>请求80端口，可能出现permission 问题（办法：先关掉selinux，setenforce 0 ）</p>
<p><strong>收集Django中的静态资源：</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">项目名/settings.py（项目配置，STATIC_ROOT设置收集静态资源的路径）：</span><br><span class="line">STATIC_URL = &apos;/static/&apos;</span><br><span class="line">STATICFILES_DIRS = [os.path.join(BASE_DIR, &apos;static&apos;)]</span><br><span class="line"># 指定收集静态文件的路径 (需要修改/var/...目录的操作权限)</span><br><span class="line">STATIC_ROOT=&apos;./static&apos;</span><br><span class="line"></span><br><span class="line"># 收集资源</span><br><span class="line">$ python manage.py collectstatic</span><br></pre></td></tr></table></figure>


    </div>

    

    

    <!-- Comments -->
    

</div>
        </section>

    </div>
</div>


</div>

<!-- Footer -->
<div class="push"></div>

<footer class="footer-content">
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 footer-about">
                <h2>About</h2>
                <p>
                    This theme was developed by <a href="https://github.com/klugjo">Jonathan Klughertz</a>. The source code is available on Github. Create Websites. Make Magic.
                </p>
            </div>
            
    <div class="col-xs-6 col-sm-6 col-md-3 col-lg-3 recent-posts">
        <h2>Recent Posts</h2>
        <ul>
            
            <li>
                <a class="footer-post" href="/blog/2019/08/12/如何快速搭建hexo技术博客/">如何快速搭建hexo技术博客</a>
            </li>
            
            <li>
                <a class="footer-post" href="/blog/2019/08/12/二进制字典数据处理/">二进制字典数据处理</a>
            </li>
            
            <li>
                <a class="footer-post" href="/blog/2019/08/12/Dos-命令手记/">Dos-命令手记</a>
            </li>
            
            <li>
                <a class="footer-post" href="/blog/2019/08/12/Mysql-命令手记/">Mysql-命令手记</a>
            </li>
            
        </ul>
    </div>



            
        </div>
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <ul class="list-inline footer-social-icons">
                    
                    <li class="list-inline-item">
                        <a href="https://github.com/klugjo/hexo-theme-alpha-dust">
                            <span class="footer-icon-container">
                                <i class="fa fa-github"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://twitter.com/?lang=en">
                            <span class="footer-icon-container">
                                <i class="fa fa-twitter"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://www.facebook.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-facebook"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://www.instagram.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-instagram"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://dribbble.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-dribbble"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://plus.google.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-google-plus"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://www.behance.net/">
                            <span class="footer-icon-container">
                                <i class="fa fa-behance"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="https://500px.com/">
                            <span class="footer-icon-container">
                                <i class="fa fa-500px"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="mailto:test@example.com">
                            <span class="footer-icon-container">
                                <i class="fa fa-envelope-o"></i>
                            </span>
                        </a>
                    </li>
                    
                    
                    <li class="list-inline-item">
                        <a href="\#">
                            <span class="footer-icon-container">
                                <i class="fa fa-rss"></i>
                            </span>
                        </a>
                    </li>
                    
                </ul>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <div class="footer-copyright">
                    @Untitled. All right reserved | Design & Hexo <a href="http://www.codeblocq.com/">Jonathan Klughertz</a>
                </div>
            </div>
        </div>
    </div>
</footer>

<!-- After footer scripts -->

<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Tween Max -->
<script src="//cdnjs.cloudflare.com/ajax/libs/gsap/1.18.5/TweenMax.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Custom JavaScript -->
<script src="/blog/js/main.js"></script>

<!-- Disqus Comments -->



</body>

</html>